<?php

namespace App\Http\Controllers\Home;

use App\Notifications\RegisterNotify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function __construct () {
        //只有游客(未登录用户)除了logout不能访问，其余方法可以访问
        $this->middleware('guest',[
            'except'=>['logout'],
        ]);
    }


    public function login(){
        return view('home.login.login');
    }
    public function store(Request $request){
        $request->validate([
            'account'=>'required',
            'password'=>'required|min:5'
        ],[
            'account.required'=>'账号不能为空',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能小于5位'
        ]);
        if(filter_var($request->account,FILTER_VALIDATE_EMAIL)){
            $data['email']=$request->account;
        }else{
            $data['mobile']=$request->account;
        }
        $data['password']=$request->password;
//        dd($data);
        if(\Auth::attempt($data,$request->remember)){
            return redirect('/')->with('success','登陆成功');
        }
        return back()->with('error','账号或密码不正确');
    }
    public function logout(){
        \Auth::logout();
        return back();
    }
}
